package com.campus_activity.campus_activity.controller;


import cn.hutool.core.bean.BeanUtil;
import com.campus_activity.campus_activity.common.PageResult;
import com.campus_activity.campus_activity.common.R;
import com.campus_activity.campus_activity.domain.dto.ActivityDTO;
import com.campus_activity.campus_activity.domain.dto.ActivityPageQueryDTO;
import com.campus_activity.campus_activity.domain.vo.ActivityDetailVO;
import com.campus_activity.campus_activity.domain.vo.ActivityTicketVO;
import com.campus_activity.campus_activity.domain.vo.ActivityVO;
import com.campus_activity.campus_activity.domain.vo.ParticipateStatusVO;
import com.campus_activity.campus_activity.service.ActivityService;
import com.campus_activity.campus_activity.service.UserActivityService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.models.annotations.OpenAPI30;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author icyrain11
 * @version 1.8
 */
@Tag(name = "活动接口")
@RestController
@RequestMapping("/activity")
@RequiredArgsConstructor
public class ActivityController {

    private final ActivityService activityService;

    private final UserActivityService userActivityService;


    @Operation(summary = "列出活动")
    @PostMapping("/list")
    public R<PageResult<ActivityVO>> listActivity(@RequestBody ActivityPageQueryDTO activityPageQueryDTO) {
        //分页查询
        PageResult<ActivityVO> pageResult = activityService.listActivity(activityPageQueryDTO);
        return R.success(pageResult);
    }

    @Operation(summary = "查看活动详情")
    @GetMapping("/detail/{id}")
    public R<ActivityDetailVO> getActivityDetail(@PathVariable Long id) {
        //如果以及报名成功则返回报名成功信息否则则进行报名
        ActivityDetailVO activityDetailVO = activityService.getActivityDetail(id);
        return R.success(activityDetailVO);
    }

    @Operation(summary = "通过频道列出活动")
    @PostMapping("/list/channel")
    public R<?> listActivityByChannel(@RequestBody ActivityPageQueryDTO activityPageQueryDTO,
                                      @RequestParam("id") Long id) {

        return R.success();
    }

    @Operation(summary = "发表活动")
    @PostMapping("/publish")
    public R<?> publishActivity(@RequestBody ActivityDTO activityDTO) {
        //TODO   @RequestPart("files") MultipartFile[] multipartFiles
        //TODO 上传图片 开启文件上传需要删除@RequestBody 二者无法兼容详情了解HTTPS
        boolean flag = activityService.publishActivity(activityDTO);
        if (!flag) {
            return R.fail("发表活动失败!");
        }
        return R.success("发布活动成功!");
    }

    @Operation(summary = "取消活动")
    @PostMapping("/cancel")
    public R<Void> cancelActivity(@RequestParam("id") Long id) {
        //取消活动
        boolean flag = activityService.cancelActivity(id);
        if (!flag) {
            return R.fail("取消失败!");
        }
        return R.success();

    }

    @Operation(summary = "查看历史参加的活动")
    @PostMapping("/list/history")
    public R<?> listActivityHistory(@RequestBody ActivityPageQueryDTO activityPageQueryDTO) {
        PageResult<ActivityVO> pageResult
                = activityService.listActivityHistory(activityPageQueryDTO);
        return R.success(pageResult);
    }


    @Operation(summary = "列出历史发布的活动")
    @PostMapping("/list/history/publish")
    public R<?> listActivityPublishHistory(@RequestBody ActivityPageQueryDTO activityPageQueryDTO) {
        //查询历史发布的活动
        PageResult<ActivityVO> pageResult = activityService.
                listActivityPublishHistory(activityPageQueryDTO);
        return R.success(pageResult);
    }

    @Operation(summary = "参加活动")
    @PostMapping("/participate")
    public R<Void> participateActivity(@RequestParam("id") Long id) {
        //参加活动
        boolean flag = userActivityService.participateActivity(id);
        if (!flag) {
            return R.fail("参加失败!");
        }
        return R.success();
    }

    @Operation(summary = "查询该活动用户状态")
    @PostMapping("/participate/status")
    public R<ParticipateStatusVO> getParticipateStatus(@RequestParam("id") Long id) {
        ParticipateStatusVO participateStatusVO =
                userActivityService.getParticipateStatus(id);
        return R.success(participateStatusVO);
    }

    @Operation(summary = "查询活动凭证")
    @GetMapping("/ticket")
    public R<ActivityTicketVO> getActivityTicket(@RequestParam("id") Long id) {
        //获取活动凭证
        ActivityTicketVO activityTicketVO = activityService.getActivityTicket(id);
        return R.success(activityTicketVO);
    }


    @Operation(summary = "退出活动")
    @PostMapping("/exit")
    public R<Void> exitActivity(@RequestParam("id") Long id) {
        boolean flag = activityService.exitActivity(id);
        if (!flag) {
            return R.fail("退出失败");
        }
        return R.success();
    }


}
